package cn.ad.server.job.adapter.dbaccess;

import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import cn.ad.server.job.adapter.persistence.job.StrategyWarmUpServant.StrategyGoupMap;
import cn.ad.server.domain.ad.AdMaterial;
import cn.ad.server.domain.ad.AdMaterialAudit;
import cn.ad.server.domain.ad.AdMaterialGroup;
import cn.ad.server.domain.ad.AdSpace;
import cn.ad.server.domain.ad.AdTemplateSpec;
import cn.ad.server.domain.ad.Campaign;
import cn.ad.server.domain.ad.Creative;
import cn.ad.server.domain.ad.Strategy;

/**
 * 组装后的数据容器 使用setXXX操作会导致失效数据 只有一个线程更改数据。多个线程获取数据。
 */
@Component
public class AssembledCachedDataContainer{
	static final Logger logger = LoggerFactory.getLogger(AssembledCachedDataContainer.class);

	// id 对应广告位包含所有的信息
	private final ConcurrentHashMap<Long, AdSpace> adSpace = new ConcurrentHashMap<>();

	// 平台id 对应策略set
	private final ConcurrentHashMap<Integer, CopyOnWriteArraySet<Strategy>> platformStrategiesMap = new ConcurrentHashMap<>();

	// 和 platformStrategiesMap 相对应
	private final ConcurrentHashMap<Integer, CopyOnWriteArraySet<Integer>> platformStrategyIdMap = new ConcurrentHashMap<>();
	// private CopyOnWriteArraySet<Integer> adviewStrategyIds = new
	// CopyOnWriteArraySet<>();

	// private ConcurrentHashMap<Integer, Integer> strategyCampaignIdMap = new
	// ConcurrentHashMap<>();
	// 订单Id 对应创意set
	private final ConcurrentHashMap<Integer, CopyOnWriteArraySet<Creative>> creativesByCampaignIdMap = new ConcurrentHashMap<>();

	// tagId 对应一个 模版Id
	private final ConcurrentHashMap<String, Integer> tagIdAdTemplateIdMap = new ConcurrentHashMap<>();

	// 根据Id对应其Strategy
	private final ConcurrentHashMap<Long, Strategy> strategies = new ConcurrentHashMap<>();

	// 根据Id对应其AdMaterial
	private final ConcurrentHashMap<Long, AdMaterial> adMaterials = new ConcurrentHashMap<>();

	// 根据创意Id对应AdMaterial集合
	private final ConcurrentHashMap<Long, CopyOnWriteArraySet<AdMaterial>> adMaterialsByCreativeIdMap = new ConcurrentHashMap<>();

	// 根据创意Id对应AdMaterialGroup集合
	private final ConcurrentHashMap<Long, CopyOnWriteArraySet<AdMaterialGroup>> adMaterialGroupsByCreativeIdMap = new ConcurrentHashMap<>();

	// 根据创意Id 和 模板id 对应AdMaterialGroup集合
	// 未实现
	private ConcurrentHashMap<String, CopyOnWriteArraySet<AdMaterialGroup>> adMaterialGroupsByCreativeIdTamplatIdMap = new ConcurrentHashMap<>();

	@Deprecated
	// 平台下 {创意｛素材组set｝} 审核通过 依赖上面属性adMaterialGroupsByCreativeIdMap 和 adMaterials
	private ConcurrentHashMap<Integer, ConcurrentHashMap<Integer, CopyOnWriteArraySet<AdMaterialGroup>>> auditPassedAdmaterialGroups = new ConcurrentHashMap<>();// 审核通过的。key为平台。

	@Deprecated
	// 平台下{ 创意｛素材set｝} 审核通过 依赖上面属性adMaterialGroupsByCreativeIdMap 和 adMaterials
	private ConcurrentHashMap<Integer, ConcurrentHashMap<Integer, CopyOnWriteArraySet<AdMaterial>>> auditPassedAdmaterials = new ConcurrentHashMap<>();// 审核通过的。key为平台。

	
	private  ConcurrentHashMap<Integer, StrategyGoupMap> mapStrategyGroupAll = new ConcurrentHashMap<>();
	
	// ---------------AssembledDataService-----------top-------
	// ok
	public AdSpace fetchAdSpaceById(Integer adSpaceId) {
		return adSpace.get(adSpaceId);
	}

	// ok
	public Set<Strategy> fetchStrategiesByPlatformId(Integer platformId) {
		return platformStrategiesMap.get(platformId);
	}

	// ok
	public Campaign fetchCampaignById(Integer campaignId) {
		return DbDataContainer.getCampaignRefer(campaignId);
	}

	// ok
	public Set<Creative> fetchCreativesByCampaignId(Integer campaignId) {
		return creativesByCampaignIdMap.get(campaignId);
	}

	// ok
	public Integer fetchTemplateIdByTagId(String tagId) {
		return tagIdAdTemplateIdMap.get(tagId);
	}

	// ok
	public Strategy fetchStrategyById(Integer strategyId) {
		return strategies.get(strategyId);
	}

	// ok
	public AdMaterial fetchAdMaterialById(Integer adMaterialId) {
		return adMaterials.get(adMaterialId);
	}

	// ok
	public Set<AdMaterialGroup> fetchAdMaterialGroupsByCreativeId(Integer creativeId) {
		return adMaterialGroupsByCreativeIdMap.get(creativeId);
	}

	// ok
	public Set<AdMaterial> fetchAdMaterialsByCreativeId(Integer creativeId) {
		return adMaterialsByCreativeIdMap.get(creativeId);
	}

	// 废弃待删除
	public Set<AdMaterial> fetchAdMaterialsPassedByCreativeId(Integer creativeId, Integer platformId) {
		ConcurrentHashMap<Integer, CopyOnWriteArraySet<AdMaterial>> creatives = auditPassedAdmaterials.get(platformId);
		if (creatives == null || creatives.isEmpty()) {
			return null;
		}
		return creatives.get(creativeId);
	}

	// 废弃待删除
	public Set<AdMaterialGroup> fetchAdMaterialGroupPassedByCreativeId(Integer creativeId, Integer platformId) {
		ConcurrentHashMap<Integer, CopyOnWriteArraySet<AdMaterialGroup>> creatives = auditPassedAdmaterialGroups
				.get(platformId);
		if (creatives == null || creatives.isEmpty()) {
			return null;
		}
		return creatives.get(creativeId);
	}

	public AdMaterialAudit fetchAdmaterialAuditPassedByCreativeId(Integer creativeId, Integer platformId) {
		String key = platformId + "c" + creativeId;// "c"和数据库中对应。
		return DbDataContainer.getAdMaterialAuditPassedRefer(key);
	}

	public AdMaterialAudit fetchAdmaterialAuditPassedByAdmaterialId(Integer admaterialId, Integer platformId) {
		String key = platformId + "m" + admaterialId;// "m"和数据库中对应。
		return DbDataContainer.getAdMaterialAuditPassedRefer(key);
	}

	public AdTemplateSpec fetchAdTemplateSpecById(Integer adTemplateSpecId) {
		return DbDataContainer.getAdTemplateSpecRefer(adTemplateSpecId);
	}

	public String fetchSysConfigValue(String key) {
		return DbDataContainer.getSysConfigByKeyRefer(key);
	}

	public boolean isPassedByCreativeId(Integer creativeId, Integer platformId) {
		if (fetchAdmaterialAuditPassedByCreativeId(creativeId, platformId)==null) {			
			return false;
		}
		return true;
	}

	public boolean isPassedByAdmaterialId(Integer admaterialId, Integer platformId) {
		if (fetchAdmaterialAuditPassedByAdmaterialId(admaterialId, platformId)==null) {
			return false;
		}
		return true;
	}

	// ---------------AssembledDataService-----------bot-------

	public ConcurrentHashMap<Long, AdSpace> getAdSpace() {
		return adSpace;
	}

	public ConcurrentHashMap<String, Integer> getTagIdAdTemplateIdMap() {
		return tagIdAdTemplateIdMap;
	}

	public ConcurrentHashMap<Integer, ConcurrentHashMap<Integer, CopyOnWriteArraySet<AdMaterialGroup>>> getAuditPassedAdmaterialGroups() {
		return auditPassedAdmaterialGroups;
	}

	public void setAuditPassedAdmaterialGroups(
			ConcurrentHashMap<Integer, ConcurrentHashMap<Integer, CopyOnWriteArraySet<AdMaterialGroup>>> auditPassedAdmaterialGroups) {
		this.auditPassedAdmaterialGroups = auditPassedAdmaterialGroups;
	}

	public ConcurrentHashMap<Integer, ConcurrentHashMap<Integer, CopyOnWriteArraySet<AdMaterial>>> getAuditPassedAdmaterials() {
		return auditPassedAdmaterials;
	}

	public void setAuditPassedAdmaterials(
			ConcurrentHashMap<Integer, ConcurrentHashMap<Integer, CopyOnWriteArraySet<AdMaterial>>> auditPassedAdmaterials) {
		this.auditPassedAdmaterials = auditPassedAdmaterials;
	}

	public ConcurrentHashMap<Integer, CopyOnWriteArraySet<Strategy>> getPlatformStrategiesMap() {
		return platformStrategiesMap;
	}

	public ConcurrentHashMap<Integer, CopyOnWriteArraySet<Integer>> getPlatformStrategyIdMap() {
		return platformStrategyIdMap;
	}

	public ConcurrentHashMap<Integer, CopyOnWriteArraySet<Creative>> getCreativesByCampaignIdMap() {
		return creativesByCampaignIdMap;
	}

	public ConcurrentHashMap<Long, Strategy> getStrategies() {
		return strategies;
	}

	public ConcurrentHashMap<Long, AdMaterial> getAdMaterials() {
		return adMaterials;
	}

	public ConcurrentHashMap<Long, CopyOnWriteArraySet<AdMaterial>> getAdMaterialsByCreativeIdMap() {
		return adMaterialsByCreativeIdMap;
	}

	public ConcurrentHashMap<Long, CopyOnWriteArraySet<AdMaterialGroup>> getAdMaterialGroupsByCreativeIdMap() {
		return adMaterialGroupsByCreativeIdMap;
	}

	public ConcurrentHashMap<String, CopyOnWriteArraySet<AdMaterialGroup>> getAdMaterialGroupsByCreativeIdTamplatIdMap() {
		return adMaterialGroupsByCreativeIdTamplatIdMap;
	}

	public void setAdMaterialGroupsByCreativeIdTamplatIdMap(
			ConcurrentHashMap<String, CopyOnWriteArraySet<AdMaterialGroup>> adMaterialGroupsByCreativeIdTamplatIdMap) {
		this.adMaterialGroupsByCreativeIdTamplatIdMap = adMaterialGroupsByCreativeIdTamplatIdMap;
	}

	public ConcurrentHashMap<Integer, StrategyGoupMap> getMapStrategyGroupAll() {
		return mapStrategyGroupAll;
	}

	public void setMapStrategyGroupAll(ConcurrentHashMap<Integer, StrategyGoupMap> mapStrategyGroupAll) {
		this.mapStrategyGroupAll = mapStrategyGroupAll;
	}

}
